﻿@using Smartstore.Web.Models.Orders;
@using Smartstore.Core.Catalog.Products;

@model IEnumerable<OrderDetailsModel>

@{
    Layout = "_Print";

    Assets.AppendTitleParts(T("PageTitle.OrderDetails"));

    var pdfMode = ViewBag.PdfMode == true;
    if (!pdfMode)
    {
        Assets.BodyAttributes.AppendCssClass("screen");
    }

    HtmlString JoinValues(string[] values, string separator = " ")
    {
        var normalized = values.Where(x => x.HasValue());
        return new HtmlString(string.Join(separator, normalized));
    }

    async Task OrderLineAsync(OrderDetailsModel.OrderItemModel item, bool showSku)
    {
        <tr>
            <td>
                <div sm-language-attributes-for="item.ProductName">
                    @item.ProductName
                </div>
                <div sm-if="item.AttributeInfo.HasValue()" class="attributes text-small">
                    @Html.Raw(item.AttributeInfo)
                </div>
            </td>
            <td sm-if="showSku">
                @item.Sku
            </td>
            <td class="text-center">
                @item.Quantity
                @item.QuantityUnit
            </td>
            <td class="text-right">
                @item.UnitPrice
            </td>
            <td class="text-right">
                @item.SubTotal
            </td>
        </tr>
    }

    async Task BundleItemsAsync(OrderDetailsModel.OrderItemModel parentItem, bool showSku)
    {
        if (parentItem.BundleItems == null)
        {
            return;
        }

        foreach (var item in parentItem.BundleItems.OrderBy(x => x.DisplayOrder))
        {
            <tr class="child-row">
                <td>
                    <div class="associated-product">
                        <div dir="auto">
                            @item.ProductName
                        </div>
                        <div sm-if="item.AttributeInfo.HasValue()" class="attributes text-small">
                            @Html.Raw(item.AttributeInfo)
                        </div>
                    </div>
                </td> 
                <td sm-if="showSku">&nbsp;</td>
                <td class="text-center text-small">
                    @Html.Raw(parentItem.BundlePerItemShoppingCart ? item.Quantity.ToString() : "&nbsp;")
                </td>
                <td class="text-right">
                    <span class="text-small">@Html.Raw(item.PriceWithDiscount > 0 ? item.PriceWithDiscount : "&nbsp;")</span>
                </td>
                <td>&nbsp;</td>
            </tr>
        }
    }
}

<div class="container">
    @foreach (var order in Model)
    {
        var colWidths = order.ShowSku ? new int[] { 45, 20, 5, 15, 15 } : new int[] { 65, 0, 5, 15, 15 };

        var company = order.MerchantCompanyInfo;
        var address = order.BillingAddress;

        <div class="document">
            <zone name="invoice_top" model="@order"></zone>

            <!-- Address & Info -->
            <div class="clearfix" style="margin-bottom: 100px">
                <div class="order-address mt-5 pull-left float-left">
                    <div>
                        <span class="header text-smallest">
                            @{
                                var city = JoinValues(new string[] { company.ZipCode, company.City }).ToString();
                                var country = JoinValues(new string[] { order.MerchantCompanyCountryName, company.StateName }, ", ").ToString();
                            }
                            @JoinValues(new string[] { company.CompanyName, company.Street, company.Street2, city, country }, " &middot; ")
                        </span>
                    </div>
                    <div class="mt-2">
                        @Html.Raw(address.FormattedAddress)
                    </div>
                </div>

                <zone name="invoice_orderinfo_top" model="@order"></zone>

                <div class="order-info text-small pull-right float-right">
                    <table>
                        <tr>
                            <td class="label">@T("PDFInvoice.Order#"):</td>
                            <td class="value">@order.OrderNumber</td>
                        </tr>
                        <tr sm-if="order.PurchaseOrderNumber.HasValue()">
                            <td class="label">@T("Order.PurchaseOrderNumber"):</td>
                            <td class="value">@order.PurchaseOrderNumber</td>
                        </tr>
                        <tr>
                            <td class="label">@T("Common.Date"):</td>
                            <td class="value">@order.CreatedOn.ToShortDateString().ReplaceNativeDigits()</td>
                        </tr>
                        <tr sm-if="order.ShippingMethod.HasValue()">
                            <td class="label">@T("Order.ShippingMethod"):</td>
                            <td class="value">@order.ShippingMethod</td>
                        </tr>
                        <tr>
                            <td class="label">@T("Order.PaymentMethod"):</td>
                            <td class="value">@order.PaymentMethod</td>
                        </tr>

                        @if (order.PaymentMethodSystemName.Equals("Payments.DirectDebit"))
                        {
                            <tr sm-if="order.DirectDebitAccountHolder.HasValue()">
                                <td class="label">&nbsp;</td>
                                <td class="value">@order.DirectDebitAccountHolder</td>
                            </tr>
                            <tr sm-if="order.DirectDebitAccountNumber.HasValue()">
                                <td class="label">&nbsp;</td>
                                <td class="value">@order.DirectDebitAccountNumber</td>
                            </tr>
                            <tr sm-if="order.DirectDebitBankName.HasValue()">
                                <td class="label">&nbsp;</td>
                                <td class="value">@order.DirectDebitBankName</td>
                            </tr>
                            <tr sm-if="order.DirectDebitIban.HasValue()">
                                <td class="label">&nbsp;</td>
                                <td class="value">@order.DirectDebitIban</td>
                            </tr>
                            <tr sm-if="order.DirectDebitBIC.HasValue()">
                                <td class="label">&nbsp;</td>
                                <td class="value">@order.DirectDebitBIC</td>
                            </tr>
                        }

                    </table>

                    <zone name="invoice_orderinfotable_after" model="@order"></zone>

                </div>

                <zone name="invoice_orderinfo_bottom" model="@order"></zone>
            </div>

            <!-- Order lines -->
            <zone name="invoice_lines_before" model="@order"></zone>

            <h3>@T("PDFInvoice.Product(s)")</h3>

            <div class="table-responsive">
                <table class="table order-summary">
                    <thead>
                        <tr>
                            <th style="width: @(colWidths[0])%">@T("PDFInvoice.ProductName")</th>
                            @if (order.ShowSku)
                            {
                                <th style="width: @(colWidths[1])%">@T("PDFInvoice.SKU")</th>
                            }
                            <th class="text-center" style="width: @(colWidths[2])%">@T("PDFInvoice.ProductQuantity")</th>
                            <th class="text-right" style="width: @(colWidths[3])%">@T("PDFInvoice.ProductPrice")</th>
                            <th class="text-right" style="width: @(colWidths[4])%">@T("PDFInvoice.ProductTotal")</th>
                        </tr>
                    </thead>
                    @foreach (var item in order.Items)
                    {
                        <tbody>
                            @{ await OrderLineAsync(item, order.ShowSku); }
                            @if (item.ProductType == ProductType.BundledProduct)
                            {
                                await BundleItemsAsync(item, order.ShowSku);
                            }
                        </tbody>
                    }
                </table>
            </div>

            <!-- Checkout attributes -->
            <zone name="invoice_checkoutattributes_before" model="@order"></zone>

            <div sm-if="order.CheckoutAttributeInfo.HasValue()" class="checkout-attributes mb-5">
                @Html.Raw(order.CheckoutAttributeInfo)
            </div>

            <!-- Order totals -->
            <zone name="invoice_ordertotals_before" model="@order"></zone>

            <div class="order-totals clearfix">
                <div class="pull-right float-right table-responsive" style="width: 40%; page-break-inside: avoid">
                    <table class="table order-totals">
                        <tr>
                            <td class="label">@T("Order.SubTotal"):</td>
                            <td class="value">@order.OrderSubtotal</td>
                        </tr>
                        <tr sm-if="order.OrderSubTotalDiscount != 0">
                            <td class="label">@T("Order.SubTotalDiscount"):</td>
                            <td class="value">@order.OrderSubTotalDiscount</td>
                        </tr>
                        <tr sm-if="order.IsShippable">
                            <td class="label">@T("Order.Shipping"):</td>
                            <td class="value">@order.OrderShipping</td>
                        </tr>
                        <tr sm-if="order.PaymentMethodAdditionalFee != 0">
                            <td class="label">@T("Order.PaymentMethodAdditionalFee"):</td>
                            <td class="value">@order.PaymentMethodAdditionalFee</td>
                        </tr>

                        @if (order.DisplayTaxRates && order.TaxRates.Count > 0)
                        {
                            foreach (var taxRate in order.TaxRates)
                            {
                                <tr>
                                    <td class="label">@taxRate.Label:</td>
                                    <td class="value">@taxRate.Value</td>
                                </tr>
                            }
                        }

                        <tr sm-if="order.DisplayTax">
                            <td class="label">@T("Order.Tax"):</td>
                            <td class="value text-right">@order.Tax</td>
                        </tr>
                        <tr sm-if="order.OrderTotalDiscount != 0">
                            <td class="label">@T("Order.TotalDiscount"):</td>
                            <td class="value">@order.OrderTotalDiscount</td>
                        </tr>

                        @if (order.GiftCards.Count > 0)
                        {
                            foreach (var gc in order.GiftCards)
                            {
                                <tr>
                                    <td class="label">@Html.Raw(T("Order.GiftCardInfo", gc.CouponCode)):</td>
                                    <td class="value">@gc.Amount</td>
                                </tr>
                            }
                        }

                        <tr sm-if="order.RedeemedRewardPoints > 0">
                            <td class="label">@T("Order.RewardPoints", order.RedeemedRewardPoints):</td>
                            <td class="value">@order.RedeemedRewardPointsAmount</td>
                        </tr>
                        <tr sm-if="order.CreditBalance > 0">
                            <td class="label">@T("ShoppingCart.Totals.CreditBalance"):</td>
                            <td class="value">@order.CreditBalance</td>
                        </tr>
                        <tr sm-if="order.OrderTotalRounding != 0">
                            <td class="label">@T("ShoppingCart.Totals.Rounding"):</td>
                            <td class="value">@order.OrderTotalRounding</td>
                        </tr>
                        <tr>
                            <td class="label">@T("Order.OrderTotal"):</td>
                            <td class="value"><strong>@order.OrderTotal</strong></td>
                        </tr>
                    </table>

                    <zone name="invoice_aftertotal" model="@order"></zone>
                </div>
            </div>

            <!-- Order notes -->
            <zone name="invoice_ordernotes_before" model="@order"></zone>

            @if (order.RenderOrderNotes && order.OrderNotes.Any())
            {
                <h3>@T("PDFInvoice.OrderNotes")</h3>

                <div class="order-notes mt-2 table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th style="width: 30%">
                                    @T("PDFInvoice.OrderNotes.CreatedOn")
                                </th>
                                <th style="width: 70%">
                                    @T("PDFInvoice.OrderNotes.Note")
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var note in order.OrderNotes)
                            {
                                <tr>
                                    <td>
                                        @note.CreatedOn.ToNativeString()
                                    </td>
                                    <td dir="auto">
                                        @Html.Raw(note.Note)
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            }

            <zone name="invoice_bottom" model="@order"></zone>
        </div>
    }
</div>